package com.atguigu.gmall.product.controller;



import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一级分类的controller层
 */
@RestController
@RequestMapping("/category1")
public class BaseCategory1Controller {
    @Autowired
    private BaseCategory1Service baseCategory1Service;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/get/{id}")
    public Result get(@PathVariable(value = "id") Long id){
        return Result.ok(baseCategory1Service.get(id));
    }

    /**
     * 查询全部一级分类信息
     * @return
     */
    @GetMapping(value = "/getAll")
    public Result get(){
        return Result.ok(baseCategory1Service.getAll());
    }

    /**
     * 新增一级分类
     * @param baseCategory1
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody BaseCategory1 baseCategory1){
        baseCategory1Service.add(baseCategory1);
        return Result.ok();
    }

    /**
     * 根据id修改一级分类信息
     * @param baseCategory1
     */
    @PutMapping("/update")
     public Result update(@RequestBody BaseCategory1 baseCategory1){
         baseCategory1Service.update(baseCategory1);
         return Result.ok();
     }

    /**
     * 根据id删除一级分类信息
     * @param id
     * @return
     */
     @DeleteMapping("/delete/{id}")
     public Result delete(@PathVariable(value = "id") Long id){
        baseCategory1Service.delete(id);
        return Result.ok();
     }

    /**
     * 根据条件查询一级分类
     * @param baseCategory1
     * @return
     */
     @PostMapping("/conditionSearch")
     public Result conditionSearch(@RequestBody BaseCategory1 baseCategory1){
         List<BaseCategory1> baseCategory1s = baseCategory1Service.conditionSearch(baseCategory1);
         return Result.ok(baseCategory1s);
     }

    /**
     * 分页查询一级分类
     * @param page 当前页码
     * @param size 一页显示的数据量
     * @return
     */
     @GetMapping("/getPage/{page}/{size}")
     public Result getPage(@PathVariable(value = "page") Integer page,
                           @PathVariable(value = "size") Integer size){
         IPage<BaseCategory1> searchPage = baseCategory1Service.getPage(page, size);
         return Result.ok(searchPage);
     }


    /**
     * 根据条件分页查询
     * @param baseCategory1 查询条件对象
     * @param page 查询页码
     * @param size 每页显示的数据量
     * @return
     */
    @PostMapping("/conditionSearch/{page}/{size}")
    public Result conditionSearch(@RequestBody BaseCategory1 baseCategory1,
                                  @PathVariable Integer page,
                                  @PathVariable Integer size){
        IPage<BaseCategory1> baseCategory1IPage = baseCategory1Service.conditionSearch(baseCategory1, page, size);
        return Result.ok(baseCategory1IPage);
    }

}
